
A$CasteCategory = mapvalues(A$C.C8_Caste, from = c('1', '2', '5',  #gen/bc/obc
                                                   '3', '4', '6', '7', '8', #sc/st/other
                                                   '888', NA), #change NA to 'NA' for expedience
                            to = c('General/BC/OBC','General/BC/OBC','General/BC/OBC',
                                   'SC/ST/Other','SC/ST/Other','SC/ST/Other','SC/ST/Other','SC/ST/Other',
                                   'NA','NA')) 

NeighCaste = A %>% group_by(A.A7_Area.Neighborhood) %>% 
  summarise(PropHiCaste = sum(CasteCategory == 'General/BC/OBC', na.rm = T) / n(),
            PropLoCaste = sum(CasteCategory == 'SC/ST/Other', na.rm = T) / n()) %>%
  mutate(CasteFrac = 1 - PropHiCaste^2 - PropLoCaste^2) %>% data.frame()

A$NeighPropHiCaste = NeighCaste[match(A$A.A7_Area.Neighborhood, Neigh$A.A7_Area.Neighborhood),'PropHiCaste']
A$NeighPropLoCaste = NeighCaste[match(A$A.A7_Area.Neighborhood, Neigh$A.A7_Area.Neighborhood),'PropLoCaste']
A$NeighCasteFrac = NeighCaste[match(A$A.A7_Area.Neighborhood, Neigh$A.A7_Area.Neighborhood),'CasteFrac']
########################################################################################################################################

########################################################################################################################################
#Now neighborhood social density
rm(list = ls(pattern='^lm'))
rm(list = ls(pattern='^out'))

#Table G1 for paper
lm1 = ( lm('ListOut ~ ListFavor*NeighDens + ListFavor*Age + ListFavor*Male + ListFavor*AssetSum + 
           ListFavor*C.C50_Years.Of.Schooling. + ListFavor*C.C14_Permanent.Residence.of.Jaipur. +
           ListFavor*factor(C.C8_Caste) + ListFavor*factor(C.C10_Mother.Tongue.) + ListFavor*factor(C.C6_Religion)',
           data = A[which(A$ListTreat != 2),]) ) #
lm0 = ( lm('ListOut ~ ListFavor*NeighDens',
           data = A[which(A$ListTreat != 2),]) ) #

out0 = coeftest(lm0, cluster.vcov(lm0, A[which(A$ListTreat != 2),'A.A7_Area.Neighborhood'] ) )
out1 = coeftest(lm1, cluster.vcov(lm1, A[which(A$ListTreat != 2),'A.A7_Area.Neighborhood'] ) )

out = stargazer(out0,out1, keep = c('^ListFavor$','^NeighDens$','^ListFavor:NeighDens$'),
                covariate.labels = c('Individual Favors Treatment','Neighborhood Social Density',
                                     'Treatment * Density'),
                dep.var.caption = "", header = F,
                add.lines = list( c("Demographic Controls", "No", "Yes"),
                                  c("N", nobs(lm0),nobs(lm1)) ),
                title = 'List experiment: Individual social connectedness and neighborhood density')
writeLines(out,con = paste0('../results/G1_',(Sys.time() %>% str_replace_all(c(":" = "-", " " = "_"))),'.tex')); rm(out)